package com.linhh.convert.run;

import com.linhh.convert.utils.CsvUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lhh
 * @Date 2024-12-17-41 11:41
 */
public class ConvertCSVFile {
    public ConvertCSVFile(){}

    public static void main(String[] args) {
        String fileName = args[0];
        String outputFile = args[1];
        List<List<String>> csvLists = CsvUtil.readCSVFileData(fileName);
        List<String> stringList = new ArrayList<String>();

        int i;
        for (i=0;i < csvLists.size();++i){
            stringList.add(((List)csvLists.get(i)).toString());
        }
        for (i =0 ;i<stringList.size();++i){
            String str = (String) stringList.get(i);
            String covertStr = str.replaceAll(",","#+#");
            covertStr = covertStr +"#+#";
            String convertstr2 = covertStr.replaceAll("\\[", "");
            String convertStr3 = convertstr2.replaceAll("]", "");
            writeTxtFile(outputFile,convertStr3);
        }
    }
    private static void writeTxtFile(String outfileCsv,String convertStr)
    {
        try {
            File file = new File(outfileCsv);
            if (!file.exists()){
                file.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
            writer.write(convertStr);
            writer.newLine();
            writer.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
